﻿//-----------------------------------------------------------------------
// <copyright file="Exists.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Mark Phillips</author>
// <email>v-mphill@microsoft.com</email>
// <date>2004-05-24</date>
// <summary>Returns TRUE if a virtual machine exists.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.VirtualServer.VirtualMachine
{
    #region Using directives
    using System;
    using System.Globalization;
    using System.Reflection;
    using System.Collections;
    using System.Runtime.InteropServices;
    using Microsoft.Sdc.Tasks.VirtualServer;
    using Microsoft.Build.Framework;
    #endregion

    /// <summary>
    /// Returns TRUE if a virtual machine exists.
    /// </summary>          
    /// <remarks>
    /// <code><![CDATA[
    /// <VirtualServer.VirtualMachine.Exists
    ///             MachineName="machineName"
    ///             ServerName="serverName">
    /// </VirtualServer.VirtualMachine.Exists>
    /// ]]></code>
    /// <para>where:</para>
    /// <para><i>machineName (Required)</i></para>
    /// <para>The name of the virtual machine to be stopped.</para>
    /// <para><i>serverName</i></para>
    /// <para>Set the <i>serverName</i> property to specify a remote server to connect to. Defaults to connect locally.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <VirtualServer.VirtualMachine.Exists
    ///             MachineName="myMachine"
    ///             Timeout="20">
    ///           <Output TaskParameter="DoesExist" PropertyName="VMExists" />
    ///         </VirtualServer.VirtualMachine.Exists> 
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class Exists : VirtualServerBase
    {
        private string serverName = String.Empty;
        private string machineName = String.Empty;
        private bool doesExist;

        /// <summary>
        /// Initializes a new instance of the VirtualServer.Exists class.
        /// </summary>
        public Exists()
        {
        }

        /// <summary>
        /// Set the <i>serverName</i> property to specify a remote server to connect to.
        /// </summary>
        /// <value>Set the <i>serverName</i> property to specify a remote server to connect to. Defaults to connect locally.</value>
        public string ServerName
        {
            get { return (this.serverName == null ? String.Empty : this.serverName); }
            set { this.serverName = value; }
        }

        /// <summary>
        /// The name of the virtual machine to be stopped
        /// </summary>
        /// <value>The name of the virtual machine to be stopped.</value>
        public string MachineName
        {
            get { return (this.machineName == null ? String.Empty : this.machineName); }
            set { this.machineName = value; }
        }

        /// <summary>
        /// Gets whether the VM does exist.
        /// </summary>
        /// <value>
        /// <b>true</b> if the VM does exist, or <b>false</b> otherwise.
        /// </value>
        [Output]
        public bool DoesExist
        {
            get { return this.doesExist; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            this.doesExist = false;

            if (this.machineName == null || this.machineName.Length == 0)
            {
                throw new TaskException("VirtualServer.InvalidMachineName", null);
            }

            ComWrapper virtualMachines = GetVirtualMachinesFromServer(this.serverName);
            
            try
            {
                ComWrapper virtualMachine = GetVirtualMachine(virtualMachines, this.machineName);
                this.doesExist = true;
            }
            catch (TaskException)
            {           
                    this.doesExist = false;
            }                
        }        
    }
}
